package com.lbj.mall.product.controller;

import com.lbj.mall.common.utils.PageUtils;
import com.lbj.mall.common.utils.R;
import com.lbj.mall.product.entity.CategoryEntity;
import com.lbj.mall.product.service.CategoryBrandRelationService;
import com.lbj.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author libingjun
 * @email 617416791@qq.com
 * @date 2022-03-27 10:44:55
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取产品树形结构
     */
    @GetMapping("/list/tree")
    public R listWithTree() {
        List<CategoryEntity> categoryEntityList = categoryService.queryListTree();
        return R.ok().put("data", categoryEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     *
     * 修改类别名字就需要修改所有与类别名字相关的数据
     */
    @Transactional
    @RequestMapping("/update")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);
        if (!StringUtils.isEmpty(category.getName())) {
            categoryBrandRelationService.updateCategoryName(category.getCatId(),category.getName());

            // TODO 修改类别名字就需要修改所有与类别名字相关的数据
        }
        return R.ok();
    }

    /**
     * 批量保存排序
     */
    @PostMapping("update/sort")
    public R updateSort(@RequestBody List<CategoryEntity> list) {
        boolean result = categoryService.updateBatchById(list);
        return result ? R.ok() : R.error();
    }

    /**
     * 删除
     * RequestBody注解表示获取请求体，而只有POST请求有请求体，所以必须发送POST请求
     * SpringMVC自动会将请求体的数据（json），转换成对应的对象
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:category:delete")
    public R delete(@RequestBody List<Long> catIds) {
        System.out.println("前台传入的数据：" + catIds);
        //categoryService.removeByIds(Arrays.asList(catIds));
        // 自定义方法，根据id删除列表
        int result = categoryService.removeMenuByIds(catIds);
        return R.ok().put("data", result);
    }
}
